home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_UDELxntp.idb / usr / freeware / src / xntp-3.4o-export / lib / hextoint.c.z / hextoint.c
C/C++ Source or Header  |  1998-01-21  |  601b  |  39 lines

  1. /*
  2.  * hextoint - convert an ascii string in hex to an unsigned
  3.  *          long, with error checking
  4.  */
  5. #include <ctype.h>
  6.  
  7. #include "ntp_stdlib.h"
  8.  
  9. int
  10. hextoint(str, ival)
  11.     const char *str;
  12.     u_long *ival;
  13. {
  14.     register u_long u;
  15.     register const char *cp;
  16.  
  17.     cp = str;
  18.  
  19.     if (*cp == '\0')
  20.         return 0;
  21.  
  22.     u = 0;
  23.     while (*cp != '\0') {
  24.         if (!isxdigit(*cp))
  25.             return 0;
  26.         if (u >= 0x10000000)
  27.             return 0;    /* overflow */
  28.         u <<= 4;
  29.         if (*cp <= '9')        /* very ascii dependent */
  30.             u += *cp++ - '0';
  31.         else if (*cp >= 'a')
  32.             u += *cp++ - 'a' + 10;
  33.         else
  34.             u += *cp++ - 'A' + 10;
  35.     }
  36.     *ival = u;
  37.     return 1;
  38. }
  39.